/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo360.mobilesafe.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    private static final String TAG = "ZipUtil";
    public static final boolean bDebug = 0;
    private static final int BUFF_SIZE = 32768;

    public static void ZipDir(File paramFile1, File paramFile2) {
        FileInputStream localFileInputStream = null;
        ZipOutputStream localZipOutputStream = null;
        localZipOutputStream = new ZipOutputStream(new FileOutputStream(paramFile2));
        if (paramFile1.isDirectory()) {
            File[] arrayOfFile = paramFile1.listFiles();
            for (int i = 0; i < arrayOfFile.length; ++i) {
                File localFile = arrayOfFile[i];
                localFileInputStream = new FileInputStream(localFile);
                localZipOutputStream.putNextEntry(new ZipEntry(paramFile1.getName() + File.separator
                        + localFile.getName()));
                int j = 0;
                while ((j = localFileInputStream.read()) != -1)
                    localZipOutputStream.write(j);
                localFileInputStream.close();
            }
        }
        localZipOutputStream.close();
    }

    public static void GzipOneFile(File paramFile1, File paramFile2) {
        if (!(paramFile1.exists()))
            return;
        GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(
                paramFile2), 32768));
        _gzipFile(paramFile1, localGZIPOutputStream);
        localGZIPOutputStream.close();
    }

    public static SizeLimitZipResult ZipDirGzip(File paramFile1, File paramFile2, int paramInt1, int paramInt2) {
        String str = paramFile2.getAbsolutePath() + ".tmp";
        File localFile = new File(str);
        boolean bool1 = localFile.delete();
        SizeLimitZipResult localSizeLimitZipResult = zipDirWithSizeLimit(paramFile1, localFile, paramInt1, paramInt2);
        if (localSizeLimitZipResult == SizeLimitZipResult.SizeLimitZipResult_NotFound)
            return localSizeLimitZipResult;
        GzipOneFile(localFile, paramFile2);
        boolean bool2 = localFile.delete();
        return localSizeLimitZipResult;
    }

    public static void unGzipZip(File paramFile1, File paramFile2) {
        try {
            File localFile = new File(paramFile1.getAbsolutePath() + ".tmp");
            unGzipFile(paramFile1, localFile);
            unZip(localFile, paramFile2);
        } catch (Exception localException) {}
    }

    public static void unZip(File paramFile1, File paramFile2) {
        BufferedOutputStream localBufferedOutputStream = null;
        ZipInputStream localZipInputStream = new ZipInputStream(
                new BufferedInputStream(new FileInputStream(paramFile1)));
        ZipEntry localZipEntry;
        while ((localZipEntry = localZipInputStream.getNextEntry()) != null) {
            File localFile = new File(paramFile2, localZipEntry.getName());
            if (localZipEntry.isDirectory()) {
                if (!(localFile.exists()))
                    localFile.mkdirs();
            }
            else {
                if (localFile.exists())
                    continue;
                localFile.getParentFile().mkdirs();
                localFile.createNewFile();
                int i = 2048;
                byte[] arrayOfByte = new byte[2048];
                FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
                localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, 2048);
                int j;
                while ((j = localZipInputStream.read(arrayOfByte, 0, 2048)) != -1)
                    localBufferedOutputStream.write(arrayOfByte, 0, j);
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
        }
        localZipInputStream.close();
    }

    private static void _gzipFile(File paramFile, GZIPOutputStream paramGZIPOutputStream) {
        byte[] arrayOfByte = new byte[32768];
        BufferedInputStream localBufferedInputStream = new BufferedInputStream(new FileInputStream(paramFile), 32768);
        int i;
        while ((i = localBufferedInputStream.read(arrayOfByte)) != -1)
            paramGZIPOutputStream.write(arrayOfByte, 0, i);
        localBufferedInputStream.close();
        paramGZIPOutputStream.flush();
    }

    private static SizeLimitZipResult zipDirWithSizeLimit(File paramFile1, File paramFile2, long paramLong1,
            long paramLong2) {
        SizeLimitZipResult localSizeLimitZipResult = SizeLimitZipResult.SizeLimitZipResult_OK;
        int i = 0;
        if ((paramFile1.exists()) && (paramFile1.isDirectory())) {
            File[] arrayOfFile = paramFile1.listFiles();
            i = (arrayOfFile.length > 0) ? 1 : 0;
            if (i != 0) {
                FileInputStream localFileInputStream = null;
                ZipOutputStream localZipOutputStream = null;
                FileOutputStream localFileOutputStream = new FileOutputStream(paramFile2);
                localZipOutputStream = new ZipOutputStream(localFileOutputStream);
                StringBuilder localStringBuilder = null;
                int j = ((paramLong2 <= 0L) && (paramLong1 <= 0L)) ? 0 : 1;
                if (j != 0)
                    localStringBuilder = new StringBuilder();
                int k = 0;
                for (int l = 0; l < arrayOfFile.length; ++l) {
                    File localFile = arrayOfFile[l];
                    localFileInputStream = new FileInputStream(localFile);
                    int i1;
                    if (j != 0) {
                        i1 = localFileInputStream.available();
                        localStringBuilder.append("[").append(l).append("/").append(arrayOfFile.length).append("]");
                        localStringBuilder.append(localFile.getName());
                        localStringBuilder.append("(").append(i1).append(")");
                        if (i1 > paramLong1) {
                            localStringBuilder.append("[TOO BIG !!!]\n");
                            localSizeLimitZipResult = SizeLimitZipResult.SizeLimitZipResult_TooBig;
                        }
                        else if (k + i1 < paramLong2) {
                            k += i1;
                            localStringBuilder.append('\n');
                        }
                        else {
                            localStringBuilder.append("[Tatol BIG !!!]\n");
                            localSizeLimitZipResult = SizeLimitZipResult.SizeLimitZipResult_TooBig;
                        }
                    }
                    else {
                        localZipOutputStream.putNextEntry(new ZipEntry(localFile.getName()));
                        i1 = 0;
                        byte[] arrayOfByte = new byte[1024];
                        while ((i1 = localFileInputStream.read(arrayOfByte, 0, 1024)) != -1)
                            localZipOutputStream.write(arrayOfByte, 0, i1);
                        localZipOutputStream.closeEntry();
                        localFileInputStream.close();
                    }
                }
                if (j != 0) {
                    if ((k == 0) && (i != 0)) {
                        localZipOutputStream.putNextEntry(new ZipEntry("common.txt"));
                        localZipOutputStream.write(localStringBuilder.toString().getBytes());
                        localZipOutputStream.closeEntry();
                    }
                    localZipOutputStream.setComment(localStringBuilder.toString());
                }
                localZipOutputStream.close();
                localFileOutputStream.close();
            }
            else {
                localSizeLimitZipResult = SizeLimitZipResult.SizeLimitZipResult_NotFound;
            }
        }
        else {
            localSizeLimitZipResult = SizeLimitZipResult.SizeLimitZipResult_NotFound;
        }
        return localSizeLimitZipResult;
    }

    public static byte[] GZip(byte[] paramArrayOfByte) {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        GZIPOutputStream localGZIPOutputStream = null;
        BufferedInputStream localBufferedInputStream = null;
        try {
            localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
            localBufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(paramArrayOfByte), 32768);
            byte[] arrayOfByte = new byte[4096];
            int i;
            while ((i = localBufferedInputStream.read(arrayOfByte)) != -1)
                localGZIPOutputStream.write(arrayOfByte, 0, i);
        } catch (Exception localException1) {
            localException1.printStackTrace();
            return null;
        } finally {
            try {
                if (localBufferedInputStream != null)
                    localBufferedInputStream.close();
                if (localGZIPOutputStream != null)
                    localGZIPOutputStream.close();
                localByteArrayOutputStream.close();
            } catch (Exception localException3) {}
        }
        return localByteArrayOutputStream.toByteArray();
    }

    public static void unGzipFile(File paramFile1, File paramFile2) {
        unGzipFile(new FileInputStream(paramFile1), paramFile2);
    }

    public static void unGzipFile(InputStream paramInputStream, File paramFile) {
        GZIPInputStream localGZIPInputStream = null;
        FileOutputStream localFileOutputStream = null;
        try {
            localGZIPInputStream = new GZIPInputStream(paramInputStream);
            localFileOutputStream = new FileOutputStream(paramFile);
            byte[] arrayOfByte = new byte[1024];
            int i;
            while ((i = localGZIPInputStream.read(arrayOfByte)) > 0)
                localFileOutputStream.write(arrayOfByte, 0, i);
            localFileOutputStream.flush();
        } finally {
            if (localGZIPInputStream != null)
                try {
                    localGZIPInputStream.close();
                } catch (Exception localException1) {}
            if (localFileOutputStream != null)
                try {
                    localFileOutputStream.close();
                } catch (Exception localException2) {}
        }
    }

    public static enum SizeLimitZipResult {
    SizeLimitZipResult_OK, SizeLimitZipResult_TooBig, SizeLimitZipResult_NotFound;
    }
}